package FrequentItemSetMining;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import util.FileHandler;

public class FrequentItemSetGenerator {
	private static GlobalTransactionList tl;
	public static float MINFREQ = 0.1f;
	public static float MINCONF = 0.3f;
	public static int MINFREQPERCENT = 10;
	public static int MINCONFPERCENT = 30;
	
	
	public static void main(String[] args) {
		readData();
		FileHandler.getInstance().deleteGlobalFile();
		for(int freq = 10; freq <= 90; freq += 10) {
			MINFREQPERCENT = freq;
			MINFREQ = (float)freq/100;
			clearAll();
			generateItemsets();
			saveResults();
			for(int conf = 10; conf <= 90; conf += 10) {
				MINCONFPERCENT = conf;
				MINCONF = (float)conf/100;
				clearRules();
				generateRules();
				saveRules();
			}
		}
	}

	private static void readData() {
		tl = GlobalTransactionList.getInstance();
		FileReader fr;		
		try {
			fr = new FileReader("courses_num.txt");
			BufferedReader in = new BufferedReader(fr);
			while (true) {
				try {
					String read = in.readLine();
					if (read == null || read.length() == 0)	break;
					System.out.println(read);
					tl.addTransaction(read);
					
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}		
	}

	private static void clearAll() {
		GlobalItemSetList.getInstance().clear();
	}
	private static void generateItemsets() {
		int totalTransactions = GlobalTransactionList.getInstance().getTotalTransactions();
		int minSupport = (int) (totalTransactions * MINFREQ);
		ArrayList<ItemSetList> itemSets = new ArrayList<ItemSetList>();
		
		//1-itemsets
		ItemSetList oneItemsets = GlobalItemList.getInstance().calculateOneItemSets(minSupport, totalTransactions);
		itemSets.add(oneItemsets);
		
		//k-itemsets		
		ItemSetList nextSize = itemSets.get(itemSets.size()-1).generateNextSize(minSupport);
		while(nextSize.getItemSetNumber() > 0) {
			itemSets.add(nextSize);
			nextSize = itemSets.get(itemSets.size()-1).generateNextSize(minSupport);
		}
		System.out.println("Itemset generation finished.");
	}
	
	private static void saveResults() {
		GlobalItemSetList gisl = GlobalItemSetList.getInstance();
		gisl.printAllFrequentItemSets();
		gisl.printAllClosedItemSets();
		gisl.printAllMaximalItemSets();
		GlobalAssociationRuleList.getInstance().printAllRules();
	}
	

	private static void saveRules() {
		System.out.println("Saving rules to files....");
		GlobalAssociationRuleList.getInstance().printAllRules();
	}

	private static void generateRules() {
		//generating association rules for all frequent itemsets
		System.out.println("Creating association rules...");
		GlobalItemSetList.getInstance().generateRules(MINCONF);
		System.out.println("Association rules created.");		
	}

	private static void clearRules() {
		GlobalAssociationRuleList.getInstance().clear();
	}

}
